#include "myhead.h"

/*
	udp发送
*/

int main()
{
	int udpsock;
	int ret;
	struct sockaddr_in bindaddr;
	bzero(&bindaddr,sizeof(bindaddr));
	bindaddr.sin_family=AF_INET;
	//inet_pton(AF_INET,"192.168.19.3",&(bindaddr.sin_addr.s_addr));
	inet_aton("192.168.19.3",&(bindaddr.sin_addr));
	bindaddr.sin_port=htons(10000);
	
	struct sockaddr_in otheraddr;
	bzero(&otheraddr,sizeof(otheraddr));
	otheraddr.sin_family=AF_INET;
	//inet_pton(AF_INET,"192.168.19.3",&(otheraddr.sin_addr.s_addr));
	inet_aton("192.168.19.3",&(otheraddr.sin_addr));
	otheraddr.sin_port=htons(20000);
	//创建udp套接字
	udpsock=socket(AF_INET,SOCK_DGRAM,0);
	if(udpsock==-1)
	{
		perror("创建udp");
		return -1;
	}
	
	//取消端口绑定的限制
	int on=1;
	setsockopt(udpsock,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on));
	
	//绑定
	ret=bind(udpsock,(struct sockaddr *)&bindaddr,sizeof(bindaddr));
	if(ret==-1)
	{
		perror("bind");
		return -1;
	}
	
	//发送信息出去
	//send()
	ret=sendto(udpsock,"hello",10,0,(struct sockaddr *)&otheraddr,sizeof(otheraddr));
	printf("sendto 返回值是:%d\n",ret);
	//关闭
	close(udpsock);
	return 0;
}